package com.trkj.exam;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.base.User;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * 正常练习
 * Created by LiZanhong on 2015/06/16.
 */
public class NormalExerciseFragment extends BaseDataRefreshFragment {
    private int userId;
    private int deptId;

    public int getUserId() {
        User user= UserService.getInstance(this.getActivity()).parseUser();
        return user == null ? 0 : user.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getDeptId() {
        User user= UserService.getInstance(this.getActivity()).parseUser();
        return user == null ? 0 : user.deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new NormalExerciseAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }


    @Override
    public String getUrl() {
        String url = MessageFormat.format(
                Constants.ApiUrl.NOTMAL_PRACTICE,
                getDeptId(),
                getUserId()
                );
        Log.i("URL", url);
        return url;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if("未提交".equals(getAdapter().getItem(position))
                        || "待批改".equals(getAdapter().getItem(position))){
            toast("该试卷还没有批改，不能查看");
            return;
        }

        Intent intent = new Intent(ExamDetailActivity.ACTION);
        Object examId = getAdapter().getItemId(position);

        if(examId != null) {
            Log.d("exam2", String.valueOf(examId));
            intent.putExtra("examId", Integer.parseInt(examId.toString()));
            startActivity(intent);
        }
    }

    class NormalExerciseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return getData().getJSONObject(position).getString("st");
        }

        @Override
        public long getItemId(int position) {
            return getData().getJSONObject(position).getIntValue("exam_id");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity())
                        .inflate(R.layout.fragment_normal_exericse_item, null);
                viewHolder = new ViewHolder();
                viewHolder.score = (TextView) convertView.findViewById(R.id.score);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.outDate = (TextView) convertView.findViewById(R.id.out_date);
                viewHolder.doDate = (TextView) convertView.findViewById(R.id.do_date);
                viewHolder.rightArrow = (TextView) convertView.findViewById(R.id.right_arrow);
                viewHolder.outDateIcon = (TextView) convertView.findViewById(R.id.out_date_icon);
                viewHolder.doDateIcon = (TextView) convertView.findViewById(R.id.do_date_icon);
                Constants.Font.setAwesomeFont(getActivity(), viewHolder.rightArrow);
                Constants.Font.setAwesomeFont(getActivity(), viewHolder.outDateIcon);
                Constants.Font.setAwesomeFont(getActivity(), viewHolder.doDateIcon);


                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray data = getData();
            String score = data.getJSONObject(position).getString("what_score");
            String status = data.getJSONObject(position).getString("st");

            if (null == score) {
                if("未提交".equals(status)) {
                    viewHolder.score.setTextColor(getColor(R.color.normal_selected));
                }else{
                    viewHolder.score.setTextColor(getColor(R.color.green));
                }
                viewHolder.rightArrow.setVisibility(View.INVISIBLE);
                viewHolder.score.setText(status);
            } else {
                viewHolder.score.setTextColor(getColor(R.color.normal_unselected));
                viewHolder.rightArrow.setVisibility(View.VISIBLE);
                viewHolder.score.setText(score + "分");
            }

            viewHolder.title.setText(data.getJSONObject(position).getString("exam_name"));
            viewHolder.outDate.setText("下发：" + data.getJSONObject(position)
                    .getString("exam_date").substring(0, 10));

            String answerTime = data.getJSONObject(position).getString("answer_time");
            if(null == answerTime){
                viewHolder.doDate.setVisibility(View.GONE);
                viewHolder.doDateIcon.setVisibility(View.GONE);
            }else {
                viewHolder.doDate.setVisibility(View.VISIBLE);
                viewHolder.doDate.setText("提交：" + answerTime);
                viewHolder.doDateIcon.setVisibility(View.VISIBLE);
            }

            viewHolder.outDateIcon.setText(
                    Constants.ResString.getString(getActivity(), R.string.fa_calendar_o));
            viewHolder.doDateIcon.setText(
                    Constants.ResString.getString(getActivity(), R.string.fa_calendar));
            return convertView;
        }

        class ViewHolder {
            TextView score;
            TextView title;
            TextView outDate;
            TextView doDate;
            TextView rightArrow;
            TextView outDateIcon;
            TextView doDateIcon;
        }
    }
}
